package com.example.yichuangyun.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemLabelBinding;
import com.example.yichuangyun.model.business.LabelModel;
import com.example.yichuangyun.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class LabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private List<LabelModel> mData;
    private final OnLabelItemClickListener onLabelItemClickListener;
    private int flag;
    private int itemCount;

    public LabelAdapter(int flag, Context context, List<LabelModel> mData, OnLabelItemClickListener onLabelItemClickListener) {
        this.flag = flag;
        this.context = context;
        this.mData = mData;
        this.onLabelItemClickListener = onLabelItemClickListener;
        itemCount = mData.size();
    }

    public void onRefresh(List<LabelModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            itemCount = Math.min(mData.size(), 9);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在这里根据不同的viewType进行引入不同的布局
        ItemLabelBinding itemLabelBinding = ItemLabelBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(itemLabelBinding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            LabelModel model = mData.get(position);
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.binding.tvLabel.setText(flag == 1 ? CommonUtil.checkText(model.getName()) : CommonUtil.checkText(model.getRemark()));

            if (model.isSelect()) {
                myViewHolder.binding.tvLabel.setTextColor(0xFF426FFF);
                myViewHolder.binding.tvLabel.setBackgroundResource(R.drawable.shape_time_bg_sel);
            } else {
                myViewHolder.binding.tvLabel.setTextColor(0xFF4A4A4A);
                myViewHolder.binding.tvLabel.setBackgroundResource(R.drawable.shape_label_bg_def);
            }
            myViewHolder.binding.tvLabel.setOnClickListener(v -> {
                if (flag == 1) {
                    for (LabelModel labelModel : mData) {
                        labelModel.setSelect(false);
                    }
                    model.setSelect(true);
                } else if (flag == 7) {
                    model.setSelect(!model.isSelect());
                } else {
                    if (model.isSelect()) {
                        model.setSelect(false);
                    } else {
                        for (LabelModel labelModel : mData) {
                            labelModel.setSelect(false);
                        }
                        model.setSelect(true);
                    }
                }
                notifyDataSetChanged();
                onLabelItemClickListener.onLabelItemClickListener(flag, model);
            });
        }
    }

    @Override
    public int getItemCount() {
        return itemCount;
    }

    public void setItemCount(int count) {
        itemCount = count;
        notifyDataSetChanged();
    }

    public void resetData(int flag) {
        for (LabelModel labelModel : mData) {
            labelModel.setSelect(false);
            if (flag == 1 && (labelModel.getName().equals(context.getString(R.string.my_follow)) || labelModel.getName().equals(context.getString(R.string.my_contacts)))) {
                labelModel.setSelect(true);
            }
            if (flag == 2 && labelModel.getNum() == 90) {
                labelModel.setSelect(true);
            }
        }
        if (flag == 7) {
            mData.get(0).setSelect(true);
        }
        notifyDataSetChanged();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemLabelBinding binding;

        public MyViewHolder(ItemLabelBinding itemLabelBinding) {
            super(itemLabelBinding.getRoot());
            binding = itemLabelBinding;
        }
    }

    public interface OnLabelItemClickListener {
        void onLabelItemClickListener(int flag, LabelModel model);
    }
}
